/**
 * 
 */
package com.wolfpire.proms.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.proms.model.Task;
import com.wolfpire.proms.model.TaskPlan;
import com.wolfpire.proms.service.TaskPlanService;
import com.wolfpire.proms.service.TaskService;
import com.wolfpire.system.common.AuthorityUtils;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.Result;
import com.wolfpire.system.common.springmvc.PrincipalArgument;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.User;

/**
 * 任务控制器
 * @author lihd
 *
 */

@Controller
@RequestMapping("/taskPlan")
public class TaskPlanController {
	
	@Resource TaskPlanService taskPlanService;
	
	@Resource TaskService taskService;
	
	@ResponseBody
	@RequestMapping("/saveTaskPlan")
	public Result saveTaskPlan(@PrincipalArgument User user, TaskPlan taskPlan) {
		try {
			this.taskPlanService.saveOrUpdateTaskPlan(taskPlan, user);
			Result result = new Result();
			result.setSuccess(true);
			result.setData(taskPlan.getId());
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "保存失败");
		}
	}
	
	@RequestMapping("/editTaskPlan")
	public ModelAndView editTaskPlan(@RequestParam(value = "id", required = false) Long id,
			@RequestParam(value = "taskId", required = false) Long taskId) {
		ModelAndView mv = new ModelAndView("proms/task/taskPlan_edit");
		TaskPlan taskPlan = null;
//		String startTimeStr = StringUtils.EMPTY;
		String endTimeStr = StringUtils.EMPTY;
		if (null != id) {
			taskPlan = this.taskPlanService.getTaskPlan(id);
//			startTimeStr = DateFormatUtils.format(taskPlan.getStartTime(), "yyyy-MM-dd");
			endTimeStr = DateFormatUtils.format(taskPlan.getEndTime(), "yyyy-MM-dd");
			taskId = taskPlan.getTaskId();
			
		} else {
			taskPlan = new TaskPlan();
			if(taskId!=null &&taskId>0)
				taskPlan.setTaskId(taskId);
		}
		if( null != taskId){
			Task task = this.taskService.get(taskId);
			String taskEndTime = DateFormatUtils.format(task.getEndTime(), "yyyy-MM-dd");
			mv.getModelMap().put("taskEndTime", taskEndTime);
			String taskStartTime = DateFormatUtils.format(task.getStartTime(), "yyyy-MM-dd");
			mv.getModelMap().put("taskStartTime", taskStartTime);
			TaskPlan filterTaskPlan =new TaskPlan();
			Page<TaskPlan> page =new Page<TaskPlan>();
			page.setPageSize(100);
			filterTaskPlan.setTaskId(taskId);
			Page<TaskPlan> taskPlans = this.taskPlanService.setPageDataTaskPlanList(page , filterTaskPlan);
			
			float f = 0;
			for (TaskPlan tp : taskPlans.getDataList()) {
				if(tp.getPlanAmount()!=null){
					if(null == id)
						f += Float.valueOf(tp.getPlanAmount());
					else if( tp.getId()!=id){
						f += Float.valueOf(tp.getPlanAmount());
					}
				}
			}
			
			mv.getModelMap().put("restAmount",Float.valueOf( task.getPlanAmount()) - f);
		}
		mv.getModelMap().put("taskPlan", taskPlan);
//		mv.getModelMap().put("startTimeStr", startTimeStr);
		mv.getModelMap().put("endTimeStr", endTimeStr);
		return mv;
	}
	
	@RequestMapping("/detailTaskPlan")
	public ModelAndView detailTaskPlan(Long id) {
		ModelAndView mv = new ModelAndView("proms/task/taskPlan_detail");
		try {
			TaskPlan taskPlan = this.taskPlanService.getTaskPlan(id);
//			String startTimeStr = DateFormatUtils.format(taskPlan.getStartTime(), "yyyy-MM-dd");
			String endTimeStr = DateFormatUtils.format(taskPlan.getEndTime(), "yyyy-MM-dd");
			mv.getModelMap().put("taskPlan", taskPlan);
//			mv.getModelMap().put("startTimeStr", startTimeStr);
			mv.getModelMap().put("endTimeStr", endTimeStr);
			return mv;
		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("/proms/common/error");
		}
	}
	
	@ResponseBody
	@RequestMapping("/deleteTaskPlan")
	public Result deleteTaskPlan(@RequestParam(value="id")Long id) {
		try {
			this.taskPlanService.delTaskPlan(id);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/queryTaskPlans")
	public EasyuiPagination<TaskPlan> queryTaskPlans(EasyuiPage<TaskPlan> page, TaskPlan filterTaskPlan) {
		Page<TaskPlan> taskPlans = this.taskPlanService.setPageDataTaskPlanList(page, filterTaskPlan);
		return new EasyuiPagination<TaskPlan>(taskPlans);
	}
	
	@RequestMapping("/planList")
	public ModelAndView planList(
			@PrincipalArgument User user,
			@RequestParam(value = "taskId", required = false) Long taskId) {
		ModelAndView mv = new ModelAndView("proms/taskPlan/taskPlan_list");
		if(taskId !=null &&taskId>0l){
			mv.getModelMap().put("taskId",taskId);
		}
		return mv;
	}
	
	@RequestMapping("/myPlanList")
	public ModelAndView myPlanList(@PrincipalArgument User user) {
		ModelAndView mv = new ModelAndView("proms/taskPlan/my_taskPlan_list");
		return mv;
	}
	
	@RequestMapping("/mgrList")
	public ModelAndView mgrList(@PrincipalArgument User user, @RequestParam Long menuId) {
		ModelAndView mv = new ModelAndView("proms/taskPlan/taskPlan_mgr_list");
		mv.getModelMap().put("userId", user.getId());
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		mv.getModelMap().put("operAuthorities", operAuthorities);
		return mv;
	}
	
	@RequestMapping("/mgrListForAdmin")
	public ModelAndView mgrListForAdmin(@PrincipalArgument User user, @RequestParam Long menuId) {
		ModelAndView mv = new ModelAndView("proms/taskPlan/taskPlan_mgr_list");
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		mv.getModelMap().put("operAuthorities", operAuthorities);
		return mv;
	}
	
	@RequestMapping("/queryForTask")
	public ModelAndView queryForTask(
			@PrincipalArgument User user,
			@RequestParam(value = "taskId", required = false) Long taskId) {
		ModelAndView mv = new ModelAndView("proms/taskPlan/list_for_task");
		if(taskId !=null &&taskId>0l){
			mv.getModelMap().put("taskId",taskId);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/queryMyPlanList")
	public EasyuiPagination<TaskPlan> queryMyPlanList(
			EasyuiPage<TaskPlan> page,
			TaskPlan filterTaskPlan,
			@PrincipalArgument User user) {
		filterTaskPlan.setPlanActorId(user.getId().toString());
		Page<TaskPlan> taskPlans = this.taskPlanService.setPageDataTaskPlanList(page, filterTaskPlan);
		return new EasyuiPagination<TaskPlan>(taskPlans);
	}
	
}
